package edu.mju.eduStatistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.mju.commonutils.ReturnCode;
import edu.mju.eduStatistics.client.UserClient;
import edu.mju.eduStatistics.entity.StatisticsDaily;
import edu.mju.eduStatistics.mapper.StatisticsDailyMapper;
import edu.mju.eduStatistics.service.StatisticsDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author xiaoxin
 * @since 2020-11-13
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UserClient userClient;

    @Override
    public void registerCount(String day) {
        //（或先查询重复，再修改）　TODO
        //添加记录之前删除表相同日期的数据　否则会有相同的记录
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated",day);
        baseMapper.delete(wrapper);

        ReturnCode returnCode = userClient.countRegister(day);
        //查询出当天注册人数
        Integer count = (Integer) returnCode.getData().get("countRegister");

        StatisticsDaily statisticsDaily = new StatisticsDaily();
        //注册人数
        statisticsDaily.setRegisterNum(count);
        //注册日期
        statisticsDaily.setDateCalculated(day);

        //以下是随机数 TODO
        statisticsDaily.setCourseNum(RandomUtils.nextInt(100,200));
        statisticsDaily.setVideoViewNum(RandomUtils.nextInt(100,200));
        statisticsDaily.setLoginNum(RandomUtils.nextInt(100,200));

        //向统计表添加当天注册人数
        baseMapper.insert(statisticsDaily);
    }

    /**
     *  图表显示统计数据
     * @param type 类型
     * @param begin 开始日期
     * @param end 结束日期
     * @return 返回日期json数据，数量json数据
     */
    @Override
    public Map<String, Object> getShowData(String type, String begin, String end) {
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated",begin,end);
        //查询date_calculated字段和type指定的字段
        wrapper.select("date_calculated",type);

        List<StatisticsDaily> statisticsDailies = baseMapper.selectList(wrapper);

        //日期list
        List<String> data_calculatedList = new ArrayList<>();
        //数量list
        List<Integer> numDataList = new ArrayList<>();

        //遍历所有数据
        for(int i = 0 ; i < statisticsDailies.size() ; i++){
            StatisticsDaily statisticsDaily = statisticsDailies.get(i);
            //封装日期list集合
            data_calculatedList.add(statisticsDaily.getDateCalculated());

            //封装数量list集合
            switch (type){
                case "register_num":
                    numDataList.add(statisticsDaily.getRegisterNum());
                    break;
                case "login_num":
                    numDataList.add(statisticsDaily.getLoginNum());
                    break;
                case "video_view_num":
                    numDataList.add(statisticsDaily.getVideoViewNum());
                    break;
                case "course_num":
                    numDataList.add(statisticsDaily.getCourseNum());
                    break;
                default:
                    break;
            }

        }

        Map<String,Object> map = new HashMap<>();
        map.put("date_calculatedList",data_calculatedList);
        map.put("numDataList",numDataList);

        return map;
    }
}
